﻿using System;
using System.Collections.Generic;
using System.IO;

namespace Simhopp.Common
{
    /// <summary>
    /// The Competition class provides a description of a diving competition.
    /// </summary>
    public class Competition
    {
        #region CONSTRUCTOR REGION



        public Competition()
        {
            DivingTypeList = new List<DivingType>();
            Divers = new List<Diver>();

            Rounds = new Round[3];



            for (int i = 0; i < 3; i++)
            {
                Round r = new Round();
                Rounds[i] = r;
            }

        }

        #endregion

        #region PROPERTY REGION

        //Creates three rounds
        public Round[] Rounds { get; set; }

        public List<Diver> Divers { get; set; }

        public List<DivingType> DivingTypeList { get; set; }

        #endregion

        #region METHOD REGION

        public void LoadDivingTypesFromFile()
        {
            //Only use this function to input all the diffrent types of jumps into the databse!
            StreamReader sr = new StreamReader("Classes/hopp2.txt");

            string line;

            while (!sr.EndOfStream)
            {
                DivingType Temp = new DivingType();

                line = sr.ReadLine();

                string[] sp = line.Split('\t');

                sp[1] = sp[1].Remove(sp[1].Length - 1);

                int TempIntHeight = Convert.ToInt16(sp[1]);
                double TempDoublePointsMultiplier = Convert.ToDouble(sp[3], System.Globalization.CultureInfo.InvariantCulture);



                //Seach through the entire list d for previous occurances of the sp[0] ID.
                //If a previous one was found, just add the newly found height and constant to its list.
                Boolean HeightConstantAdded = false;
                foreach (var TempTemp in DivingTypeList)
                {
                    if (TempTemp.ID == sp[0])
                    {
                        TempTemp.AddHeight((Heights)TempIntHeight);
                        TempTemp.AddMultiplier(TempDoublePointsMultiplier);

                        HeightConstantAdded = true;
                        break;
                    }
                }


                //This if handles the situation where a entierly new object must be added to the list.
                if (!HeightConstantAdded)
                {
                    Temp.ID = sp[0];
                    Temp.AddHeight((Heights)TempIntHeight);
                    Temp.TypeName = sp[2];
                    Temp.AddMultiplier(TempDoublePointsMultiplier);
                    DivingTypeList.Add(Temp);
                }
            }




        }

        #endregion
    }
}
